package com.guanghua.brick.html;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OptionControl extends WebControl {
	
	//private static Log logger = LogFactory.getLog(InputControl.class);
	
	private String type = "radio";
	private int newLine = 0;//0表示不换行
	private boolean labelAfter = true;
	
	private String textProperty = "text";
	private String valueProperty = "value";


	@Override
	public String buildHTML(HttpServletRequest request, HttpServletResponse response) {
		
		StringBuffer html = new StringBuffer();
		
		//有content就需要根据content来生成控件
		if (this.getContent() != null) {
			List<Map<String, String>> list = this.getContent().buildContent(request, response);
			Iterator<Map<String, String>> i = list.iterator();
			int a = 0;
			while (i.hasNext()) {
				Map<String, String> map = i.next();
				String text = map.get(this.textProperty);
				String value = map.get(this.valueProperty);
				
				if (text == null || value == null) continue;
				
				if (!this.labelAfter) html.append(text);
				
				html.append("<input type=\"")
				.append(this.getType())
				.append("\" name=\"")
				.append(this.getName())
				.append("\" value=\"")
				.append(value)
				.append("\" ")
				.append((this.isSelected(value))?"checked ":"")
				.append(this.getHtml())
				.append("/>");
				
				if (this.labelAfter) html.append(text);
				
				if (this.newLine != 0 && ++a%this.newLine == 0) html.append("<br>");
				
				html.append("\n");
			}
		} else {
			html.append("<input id=\"")
			.append(this.getId())
			.append("\" type=\"")
			.append(this.getType())
			.append("\" name=\"")
			.append(this.getName())
			.append("\" value=\"")
			.append((this.getData()!=null)?this.getData():"")
			.append("\"")
			.append(this.getHtml())
			.append("/>\n");
		}
		
		return html.toString();
	}
	
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}


	public boolean isLabelAfter() {
		return labelAfter;
	}


	public void setLabelAfter(boolean labelAfter) {
		this.labelAfter = labelAfter;
	}


	public int getNewLine() {
		return newLine;
	}


	public void setNewLine(int newLine) {
		this.newLine = newLine;
	}
	
	public String getTextProperty() {
		return textProperty;
	}


	public void setTextProperty(String textProperty) {
		this.textProperty = textProperty;
	}


	public String getValueProperty() {
		return valueProperty;
	}


	public void setValueProperty(String valueProperty) {
		this.valueProperty = valueProperty;
	}

}
